package com.liik.bomb.screen.action;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;

import com.liik.bomb.R;
import com.liik.game.mvc.object.Menu.Item;
import com.liik.game.mvc.tiled.object.Entity;
import com.liik.game.mvc.tiled.object.Tile;

public class SpriteBasedBombGameRenderer extends SimpleBombGameRenderer {

	private Bitmap tileset;
	private Rect empty[], tree[], rock[];
	private Rect hero, flame, bomb, enemy;
	private Rect destination = new Rect();
	private Paint deathPaint;

	public SpriteBasedBombGameRenderer() {
		empty = new Rect[4];
		tree  = new Rect[4];
		rock  = new Rect[4];
		empty[0] = new Rect(0, 0, 64, 64);
		tree[0]  = new Rect(64, 0, 128, 64);
		rock[0]  = new Rect(128, 0, 192, 64);
		empty[1] = new Rect(0, 64, 64, 128);
		tree[1]  = new Rect(64, 64, 128, 128);
		rock[1]  = new Rect(128, 64, 192, 128);
		empty[2] = new Rect(0, 128, 64, 192);
		tree[2]  = new Rect(64, 128, 128, 192);
		rock[2]  = new Rect(128, 128, 192, 192);
		empty[3] = new Rect(0, 192, 64, 256);
		tree[3]  = new Rect(64, 192, 128, 256);
		rock[3]  = new Rect(128, 192, 192, 256);
		
		hero = new Rect(0, 256, 64, 320);
		bomb = new Rect(0, 320, 64, 384);
		flame = new Rect(0, 384, 64, 448);
		enemy = new Rect(0, 448, 64, 512);
		
		deathPaint = new Paint();
		deathPaint.setAlpha(200);
	}

	@Override
	public void setContext(Context context) {
		Options options = new Options();
		options.inDensity = 240;
		options.inScreenDensity = 240;
		options.inTargetDensity = 240;
		tileset = BitmapFactory.decodeResource(context.getResources(), R.drawable.tileset, options);
//		int density = tileset.getDensity();
//		int width = tileset.getWidth();
//		if (width != 640) {
//			assert(false);
//		}
	}

	@Override
	protected void renderMenuItem(Item item, Canvas canvas) {
		super.renderMenuItem(item, canvas);
	}

	@Override
	protected void renderNavigationTarget(Canvas canvas, float x, float y) {
		super.renderNavigationTarget(canvas, x, y);
	}

	@Override
	protected void renderEntity(Entity entity, Canvas canvas, float x, float y, float width, float height) {
		destination.set((int) x, (int) y, (int) x + (int) width, (int) y + (int) height);
		if (entity instanceof BombEntity) {
			BombEntity bombEntity = (BombEntity) entity;
			if (bombEntity.state == com.liik.bomb.screen.action.BombEntity.State.Counting) {
				destination.set((int) x, (int) y, (int) x + (int) width, (int) y + (int) height);
				canvas.drawBitmap(tileset, bomb, destination, null);				
			} else if (bombEntity.state == com.liik.bomb.screen.action.BombEntity.State.Exploding) {
				destination.set((int) x, (int) y, (int) x + (int) width, (int) y + (int) height);
				for (int i = 0; i <= bombEntity.rangeEast; i++) {
					canvas.drawBitmap(tileset, flame, destination, null);
					destination.offset(64, 0);
				}
				destination.set((int) x, (int) y, (int) x + (int) width, (int) y + (int) height);
				for (int i = 0; i <= bombEntity.rangeWest; i++) {
					canvas.drawBitmap(tileset, flame, destination, null);
					destination.offset(-64, 0);
				}
				destination.set((int) x, (int) y, (int) x + (int) width, (int) y + (int) height);
				for (int i = 0; i <= bombEntity.rangeNorth; i++) {
					canvas.drawBitmap(tileset, flame, destination, null);
					destination.offset(0, -64);
				}
				destination.set((int) x, (int) y, (int) x + (int) width, (int) y + (int) height);
				for (int i = 0; i <= bombEntity.rangeSouth; i++) {
					canvas.drawBitmap(tileset, flame, destination, null);
					destination.offset(0, 64);
				}
			}
		} else if (entity instanceof BombermanEntity) {
			BombermanEntity bomberMan = (BombermanEntity) entity;
			if (bomberMan.deathTime == BombermanEntity.NOT_SET)
				canvas.drawBitmap(tileset, hero, destination, null);
			else {
				canvas.drawBitmap(tileset, hero, destination, deathPaint);
			}
		} else if (entity instanceof EnemyEntity) {
			EnemyEntity enemyEntity = (EnemyEntity) entity;
			if (enemyEntity.deathTime == EnemyEntity.NOT_SET)
				canvas.drawBitmap(tileset, enemy, destination, null);
			else 
				canvas.drawBitmap(tileset, enemy, destination, deathPaint);
				
		} else {
			super.renderEntity(entity, canvas, x, y, width, height);
		}

	}

	@Override
	protected void renderTile(Tile tile, Canvas canvas, float x, float y, float width, float height) {
		if (tile != null) {
			destination.set((int) x, (int) y, (int) x + (int) width, (int) y + (int) height);
			switch (tile.type) {
			case Bridge:
				break;
			case Earth:
				canvas.drawBitmap(tileset, tree[tile.subtype], destination, null);
				break;
			case Empty:
				canvas.drawBitmap(tileset, empty[tile.subtype], destination, null);
				break;
			case Wall:
				canvas.drawBitmap(tileset, rock[tile.subtype], destination, null);
				break;
			case Water:
				break;

			}
		}
	}
}
